package com.study.yangfei.mystudy.utils;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

public class MyService extends Service {
    final String TAG = MyService.class.getSimpleName();

    static public String ACTION_SERVICE_STOP = "action_service_stopg";
    static public String ACTION_SERVICE_UPDATE_SUM = "action_service_update_sum";

    static public String KEY_SUM = "sum";
    final int MSG_SUM = 10000;

    public YfBinder mBinder = new YfBinder();

    public Handler mServiceHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case MSG_SUM:{
                    Intent in = new Intent(ACTION_SERVICE_UPDATE_SUM);
                    double sum = msg.getData().getDouble(KEY_SUM);
                    in.putExtra(KEY_SUM,sum);
                    MyService.this.sendBroadcast(in);
                    break;
                }
            }
        }
    };

    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        Log.d(TAG, "onBind");
        return mBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(TAG, "onUnbind");
        return false;
    }


    @Override
    public void onCreate() {
        Log.d(TAG, "onCreate");
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy");
        Intent in = new Intent(ACTION_SERVICE_STOP);
        this.sendBroadcast(in);
        yt.stopThread();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand");
        return Service.START_STICKY;
    }

    public void serviceFun(){
        Log.d(TAG, "serviceFun");
    }

    YfThread yt;
    public void heaveyWork(){
        Log.d(TAG, "heaveyWork");
        Log.d("heaveyWork","thread id = "+Thread.currentThread().getId());
        yt = new YfThread("YfHeaveyWork");
        yt.setPriority(Thread.MIN_PRIORITY);
        yt.start();
    }

    class YfThread extends Thread{
        boolean mIsStop = false;

        public Handler mHandler;

        public YfThread(String s){
            super(s);
        }

        public void stopThread(){
            mIsStop = true;
        }

        @Override
        public void run() {
            Log.d("YfThread","thread id = "+Thread.currentThread().getId());
            double a = Math.random();
            double b = Math.random();
            double sum = 0.f;
            for(int i=1;i<Integer.MAX_VALUE;i++){
                if(mIsStop)
                    break;
                for(int j=1;j<Integer.MAX_VALUE;j++){
                    if(mIsStop)
                        break;
                    for(int k=1;k<Integer.MAX_VALUE;k++){
                        if(mIsStop)
                            break;
                        sum = a/b+a*a+b*b+1.f/(double)i+1.f/(double)j+1.f/(double)k;
                        Log.e("YfThread","sum = "+sum);
                        Message msg = new Message();
                        msg.what = MSG_SUM;
                        Bundle bn = new Bundle();
                        bn.putDouble(KEY_SUM,sum);
                        msg.setData(bn);
                        mServiceHandler.sendMessage(msg);
                        synchronized (this){
                            try {
                                this.wait(1);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
            Log.e("YfThread is stopped","final sum = "+sum);
        }
    }

    public class YfBinder extends Binder {
        public MyService getService(){
            return MyService.this;
        }
    }



}

